package com.mangguo.tingjian.content.mapper;

import com.mangguo.tingjian.content.controller.rsp.ContentSimpleRsp;
import com.mangguo.tingjian.content.dto.NameResult;
import com.mangguo.tingjian.content.entity.DwAudio;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mangguo.tingjian.user.dto.CountResult;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 音频同步表 Mapper 接口
 * </p>
 *
 * @author auto
 * @since 2019-11-24
 */
@Mapper
public interface DwAudioMapper extends BaseMapper<DwAudio> {
    @Select("select count(1) from dw_audio where mdate >= #{start} and mdate <= #{end}")
    Long countAudioBetweenDate(@Param("start")Integer start,
                               @Param("end") Integer end);

    @Select("select count(1) from dw_audio")
    Long countAudioAll();

    @Select("select sum(duration) from dw_audio where mdate >= #{start} and mdate <= #{end}")
    Long sumAudioDurationBetweenDate(@Param("start")Integer start,
                                     @Param("end") Integer end);

    @Select("select sum(duration) from dw_audio")
    Long sumAudioDurationAll();

    @Select("select audioName from dw_audio where audioId = #{audioId}")
    String getAudioNameByAudioId(@Param("audioId")Long audioId);

    @Select("<script>select audioName,audioId,albumName from dw_audio where" +
            "<if test='audioIds != null and audioIds.size()>0'> "+
            "audioId IN " +
            "<foreach item='audioId' collection='audioIds' open='(' close=')' separator=','> "+
            "#{audioId}"+
            "</foreach> AND "+
            "</if>"+
            "1=1</script>")
    List<NameResult> getAudioNamesByAudioIds(@Param("audioIds")List<Long> audioIds);

    @Select("select authorId from dw_audio where authorName = #{authorName} and authorType = 1 and authorId != 0 limit 1")
    Long getAuthorIdByAuthorName(@Param("authorName")String authorName);

    @Select("select albumName from dw_audio where audioId = #{audioId} limit 1")
    String getAlbumNameByAudioId(@Param("audioId")Long audioId);

    @Select("select count(1) from (select albumId from dw_audio where mdate >= #{start} and mdate <= #{end} group by albumId) as a")
    Long countAlbumBetweenDate(@Param("start")Integer start,
                               @Param("end") Integer end);
    @Select("select count(1) from (select albumId from dw_audio group by albumId) as a")
    Long countAlbumAll();

    @Select("select distinct audioName from dw_audio where audioName like #{namePatten} limit 5")
    List<String> queryAudioNameLike(@Param("namePatten")String namePatten);

    @Select("select distinct albumName from dw_audio where albumName like #{namePatten} limit 5")
    List<String> queryAlbumNameLike(@Param("namePatten")String namePatten);

    @Select("select distinct cmo from dw_audio where cmo like #{cmo} limit 5")
    List<String> queryCmoNameLike(@Param("cmo")String cmo);

    @Select("select distinct authorName from dw_audio where authorType = 1 and authorName like #{namePatten} limit 5")
    List<String> queryAuthorNameLike(@Param("namePatten")String namePatten);

    @Select("<script>"
            + "SELECT audioId "
            + "FROM dw_audio "
            + "<where> "
            + "<if test='audioName != null'> "
            + "audioName = #{audioName}  AND "
            + "</if>"
            + "<if test='albumName != null'> "
            + "albumName = #{albumName}  AND "
            + "</if>"
            + "1=1"
            + "</where>"

            + "limit 1"
            + "</script>")
    Long getAudioIdByAudioNameAndAlbumName(@Param("audioName")String audioName, @Param("albumName")String albumName);

    @Select("<script>"
            + "SELECT audioId "
            + "FROM dw_audio "
            + "<where> "
            + "<if test='albumName != null'> "
            + "albumName = #{albumName}  AND "
            + "</if>"
            + "<if test='cmo != null'> "
            + "cmo = #{cmo}  AND "
            + "</if>"
            + "1=1"
            + "</where>"

            + "</script>")
    List<Long> getAudioIdByAlbumNameAndCmo(@Param("albumName")String albumName, @Param("cmo")String cmo);

    @Select("<script>"
            + "SELECT albumId "
            + "FROM dw_audio "
            + "<where> "
            + "<if test='albumName != null'> "
            + "albumName = #{albumName}  AND "
            + "</if>"
            + "<if test='cmo != null'> "
            + "cmo = #{cmo}  AND "
            + "</if>"
            + "1=1 "
            + "</where>"
            + " limit 1"
            + "</script>")
    Long getAlbumIdByAlbumNameAndCmo(@Param("albumName")String albumName, @Param("cmo")String cmo);

    @Select("select audioId from dw_audio where albumId = #{albumId}")
    List<Long> getAudioIdByAlbumId(@Param("albumId")Long albumId);


    @Select("select distinct albumId from dw_audio where authorName = #{authorName} and authorType = 1")
    List<Long> getAlbumIdByAuthorName(@Param("authorName")String authorName);

    @Select("select albumName from dw_audio where albumId = #{albumId} limit 1")
    String getAlbumNameByAlbumId(@Param("albumId")Long albumId);


    @Select("select count(1) as audioNew , sum(duration) as durationNew, count(distinct albumId) as albumNew from dw_audio where mdate >= #{start} and mdate <= #{end}")
    ContentSimpleRsp getContentSimpleRspBetweenDate(@Param("start")Integer start,
                                                    @Param("end") Integer end);

    @Select("select count(1) as audioAll , sum(duration) as durationAll, count(distinct albumId) as albumAll from dw_audio")
    ContentSimpleRsp getContentSimpleRspAll();

}
